package com.ruoyi.web.controller.his;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.his.domain.OdsDoctorData;
import com.ruoyi.his.domain.vo.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.his.domain.OdsDoctorAll;
import com.ruoyi.his.service.IOdsDoctorAllService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 * 
 * @author Alan
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/his/allDoc")
public class OdsDoctorAllController extends BaseController
{
    @Autowired
    private IOdsDoctorAllService odsDoctorAllService;

    @Autowired
    private RedisCache redisCache;
    /**
     * 对外插入数据的接口
     * @param vo
     * @return
     */

    @PostMapping("/insterDoc")
    public AjaxResult insterDoc(@RequestBody OdsListDoctorAllVO vo){
        return odsDoctorAllService.insterDoc(vo);
    }

    /**
     * 医生的注册业务
     * @param odsDoctorAll
     * @return
     */

    @PostMapping("/registerDoctor")
    @Transactional
    public AjaxResult registerDocktor(@RequestBody OdsDoctorAll odsDoctorAll){
        if(odsDoctorAll.getReCount()==null){
            return AjaxResult.error("系统繁忙，稍后再试！");
        }
        //如果有重复的记录，则调用非选择医师的注册方式
//        if(odsDoctorAll.getReCount()>0){
//            return odsDoctorAllService.registerDoctorByLicenceUnSelect(odsDoctorAll);
//        }
        //没有重复的医院名称，调用选择医师的注册方式
        return odsDoctorAllService.registerDoctorByLicenceSelect(odsDoctorAll);
    }

    /**
     * 通过签名判断医院名称是否重复
     * @param signature
     * @return
     */
    @GetMapping("/checkSameName")
    public int checkSameName(@RequestParam(required = false) String signature ){
        return odsDoctorAllService.checkSameName(signature);
    }

    /**
     * 通过签名去获取医生
     * @param signature
     * @return
     */
    @GetMapping("/getDocListBySig")
    public List<OdsDoctorAll> getDocListBySig(@RequestParam(required = false) String signature ){
        return odsDoctorAllService.selectOdsDocListBySig(signature);
    }
    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('his:all:list')")
    @GetMapping("/list")
    public TableDataInfo list(OdsDoctorAll odsDoctorAll)
    {
        startPage();
        List<String> hospitalIds =new ArrayList<>();
        List<String> signatures =new ArrayList<>();
        List<Map> mapList = redisCache.getCacheList("hospitalId");
        for (Map map :mapList){
            if(map.get("hospitalId")!=null){
                hospitalIds.add(map.get("hospitalId").toString());
            }
            if(map.get("signature")!=null){
                signatures.add(map.get("signature").toString());
            }
        }
        odsDoctorAll.setHospitalIds(hospitalIds);
        odsDoctorAll.setSignatures(signatures);
        List<OdsDoctorAll> list = odsDoctorAllService.selectOdsDoctorAllList(odsDoctorAll);
        return getDataTable(list);
    }

    /**
     * 通过sig hosid reg 查询医生
     * @param odsDoctorAll
     * @return
     */
    @GetMapping("/DocTowlist")
    public TableDataInfo DocTowlist(OdsDoctorAll odsDoctorAll)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<OdsDoctorAll> list = odsDoctorAllService.selectDocTowlist(odsDoctorAll);
        int num = list.size();
        list=list.stream().skip((pageNum -1) * pageSize).limit(pageSize).collect(Collectors.toList());
        TableDataInfo rsp = new TableDataInfo();
        rsp.setCode(HttpStatus.SUCCESS);
        rsp.setRows(list);
        rsp.setTotal(num);
        return rsp;
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('his:all:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OdsDoctorAll odsDoctorAll)
    {
        List<OdsDoctorAll> list = odsDoctorAllService.selectOdsDoctorAllList(odsDoctorAll);
        ExcelUtil<OdsDoctorAll> util = new ExcelUtil<OdsDoctorAll>(OdsDoctorAll.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('his:all:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(odsDoctorAllService.selectOdsDoctorAllById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('his:all:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OdsDoctorAll odsDoctorAll)
    {
        return toAjax(odsDoctorAllService.insertOdsDoctorAll(odsDoctorAll));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('his:all:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OdsDoctorAll odsDoctorAll)
    {
        return toAjax(odsDoctorAllService.updateOdsDoctorAll(odsDoctorAll));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('his:all:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(odsDoctorAllService.deleteOdsDoctorAllByIds(ids));
    }


    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<OdsDoctorAllVo> util = new ExcelUtil<OdsDoctorAllVo>(OdsDoctorAllVo.class);
        return util.importTemplateExcel("医师多执业备案信息导入");
    }
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<OdsDoctorAllVo> util = new ExcelUtil<OdsDoctorAllVo>(OdsDoctorAllVo.class);
        List<OdsDoctorAllVo> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = odsDoctorAllService.importData(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate2")
    public AjaxResult importTemplate2()
    {
        ExcelUtil<OdsDoctorAllVo2> util = new ExcelUtil<OdsDoctorAllVo2>(OdsDoctorAllVo2.class);
        return util.importTemplateExcel("医师电子化注册系统文件");
    }

    @PostMapping("/importData2")
    public AjaxResult importData2(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<OdsDoctorAllVo2> util = new ExcelUtil<OdsDoctorAllVo2>(OdsDoctorAllVo2.class);
        List<OdsDoctorAllVo2> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = odsDoctorAllService.importData2(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

}
